package com.wrench.elevator.tida.service;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.dao.LoginDao;
import com.wrench.elevator.tida.domain.wuye.Wuye_User;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.util.WrenchUtil;

@Component
public class LoginService
{
    @Resource
    private LoginDao dao;
    
    private static final Logger logger = LoggerFactory.getLogger(LoginService.class);


    public int resetPwd(String tel,String pwd) throws WrenchException {
        if(WrenchUtil.hasNull(tel,pwd)){
            logger.error("==> param is not fund , tel = {} , pwd = {} ",tel,pwd);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        int commit=-1;
        try {
            commit=dao.resetPwd(tel, pwd);
        } catch (Exception e) {
           logger.error("==> commit=dao.resetPwd(tel, pwd) execute error , tel = {} , pwd = {} , commit = {} , error = {} ",tel,pwd,commit,e);
           throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
        }
        return commit;

    }

    public Wuye_User login(String user_tel, String user_pwd)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(user_tel, user_pwd))
        {
            logger.error("==> param is not found , user_tel = {} , user_pwd = {} ", user_tel, user_pwd);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchUtil.isVaildPhone(user_tel))
        {
            logger.error("==> phone number is not vaild , user_tel = {} ", user_tel);
            throw new WrenchException(Result.PHONE_NUMBER_IS_NOT_VAILD_CODE, Result.PHONE_NUMBER_IS_NOT_VAILD_MESSAGE);
        }
        Wuye_User user = null;
        try
        {
            user = dao.login(user_tel, user_pwd);
        }
        catch (Exception e)
        {
            logger.error("==> user=dao.login(user_tel, user_pwd) execute error , user_tel = {} , user_pwd = {} , error = {} ",
                user_tel,
                user_pwd,
                e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        
        logger.debug("==> user=dao.login(user_tel, user_pwd) execute success , user_tel = {} , user_pwd = {} , user = {} ",
            user_tel,
            user_pwd,
            user);
        return user;
        
    }
}
